<template>
  <div class="app-container">
    <div class="search-area" v-show="showSearch">
      <el-form :model="queryParams" @submit.native.prevent ref="queryForm" size="small" :inline="true" class="clearfix"
        label-width="auto">
        <el-form-item label="订单类型" prop="type">
          <el-select v-model="queryParams.type" clearable placeholder="请选择订单类型">
            <el-option label="陪玩" :value="1" />
            <el-option label="商品" :value="2" />
          </el-select>
        </el-form-item>
        <el-form-item label="订单状态" prop="status">
          <el-select v-model="queryParams.status" clearable placeholder="请选择订单状态">
            <el-option label="待支付" :value="0" />
            <el-option label="已支付" :value="1" />
            <el-option label="已退款" :value="2" />
            <el-option label="部分退款" :value="3" />
            <el-option label="已完成" :value="4" />
            <el-option label="已取消" :value="5" />
          </el-select>
        </el-form-item>
        <el-form-item label="支付状态" prop="payStatus">
          <!-- <el-input v-model="queryParams.payStatus" placeholder="请输入支付状态 0-待支付 1-已支付" clearable
            @keyup.enter.native="handleQuery" /> -->
          <el-select v-model="queryParams.payStatus" clearable placeholder="请选择支付状态">
            <el-option label="待支付" :value="0" />
            <el-option label="已支付" :value="1" />
          </el-select>
        </el-form-item>
        <el-form-item label="退款状态" prop="refundStatus">
          <!-- <el-input v-model="queryParams.refundStatus" placeholder="请输入退款状态 0-待退款 1-待审核 2-审核不通过 3-审核通过-部分退款 4-审核通过-全部退款"
            clearable @keyup.enter.native="handleQuery" /> -->
          <el-select v-model="queryParams.refundStatus" clearable placeholder="请选择退款状态">
            <el-option label="待退款" :value="0" />
            <el-option label="待审核" :value="1" />
            <el-option label="审核不通过" :value="2" />
            <el-option label="审核通过-部分退款" :value="3" />
            <el-option label="审核通过-全部退款" :value="4" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="main-content">
      <el-row :gutter="10" class="mb24">
        <el-col :span="1.5">
          <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
            v-hasPermi="['biz:order:add']">新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
            v-hasPermi="['biz:order:edit']">修改</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
            v-hasPermi="['biz:order:remove']">删除</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
      </el-row>
      <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="用户id" align="center" prop="userId" v-if="columns.userId.visible" />
        <el-table-column label="订单号" align="center" prop="orderNo" v-if="columns.orderNo.visible" />
        <el-table-column label="第三方订单号(微信)" align="center" prop="thirdOrderNo" v-if="columns.thirdOrderNo.visible" />
        <el-table-column label="订单总额" align="center" prop="totalAmount" v-if="columns.totalAmount.visible" />
        <el-table-column label="优惠金额" align="center" prop="couponAmount" v-if="columns.couponAmount.visible" />
        <el-table-column label="实付总额" align="center" prop="payAmount" v-if="columns.payAmount.visible" />
        <el-table-column label="余额支付金额" align="center" prop="balanceAmount" v-if="columns.balanceAmount.visible" />
        <el-table-column label="微信支付金额" align="center" prop="wxAmount" v-if="columns.wxAmount.visible" />
        <el-table-column label="订单类型 1-陪玩 2-商品" align="center" prop="type" v-if="columns.type.visible" />
        <el-table-column label="订单状态" align="center" prop="status" v-if="columns.status.visible">
          <template slot-scope="scope">
            <span>
              <template v-if="scope.row.status === 0">待支付</template>
              <template v-else-if="scope.row.status === 1">已支付</template>
              <template v-else-if="scope.row.status === 2">已退款</template>
              <template v-else-if="scope.row.status === 3">部分退款</template>
              <template v-else-if="scope.row.status === 4">已完成</template>
              <template v-else-if="scope.row.status === 5">已取消</template>
            </span>
          </template>
        </el-table-column>
        <el-table-column label="支付状态" align="center" prop="payStatus" v-if="columns.payStatus.visible">
          <template slot-scope="scope">
            <span>
              <template v-if="scope.row.payStatus === 0">待支付</template>
              <template v-else-if="scope.row.payStatus === 1">已支付</template>
            </span>
          </template>
        </el-table-column>
        <el-table-column label="退款状态" align="center" prop="refundStatus" v-if="columns.refundStatus.visible">
          <template slot-scope="scope">
            <span>
              <template v-if="scope.row.refundStatus === 0">待退款</template>
              <template v-else-if="scope.row.refundStatus === 1">待审核</template>
              <template v-else-if="scope.row.refundStatus === 2">审核不通过</template>
              <template v-else-if="scope.row.refundStatus === 3">审核通过-部分退款</template>
              <template v-else-if="scope.row.refundStatus === 4">审核通过-全部退款</template>
            </span>
          </template>
        </el-table-column>
        <el-table-column label="支付时间" align="center" prop="payTime" v-if="columns.payTime.visible" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.payTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="退款时间" align="center" prop="refundTime" v-if="columns.refundTime.visible" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.refundTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="数量" align="center" prop="num" v-if="columns.num.visible" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button size="mini" type="text" icon="el-icon-tickets" @click="handleUpdate(scope.row, true)"
              v-hasPermi="['biz:order:query']">查看</el-button>
            <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
              v-hasPermi="['biz:order:edit']">修改</el-button>
            <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
              v-hasPermi="['biz:order:remove']">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
        @pagination="getList" />
    </div>



    <!-- 添加或修改订单对话框 -->
    <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="open" width="850px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="auto" :disabled="disabled"
        :class="{ 'detail-disabled': disabled }">
        <el-form-item label="用户id" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户id" />
        </el-form-item>
        <el-form-item label="订单号" prop="orderNo">
          <el-input v-model="form.orderNo" placeholder="请输入订单号" />
        </el-form-item>
        <el-form-item label="第三方订单号(微信)" prop="thirdOrderNo">
          <el-input v-model="form.thirdOrderNo" placeholder="请输入第三方订单号(微信)" />
        </el-form-item>
        <el-form-item label="订单总额" prop="totalAmount">
          <el-input v-model="form.totalAmount" placeholder="请输入订单总额" />
        </el-form-item>
        <el-form-item label="优惠金额" prop="couponAmount">
          <el-input v-model="form.couponAmount" placeholder="请输入优惠金额" />
        </el-form-item>
        <el-form-item label="实付总额" prop="payAmount">
          <el-input v-model="form.payAmount" placeholder="请输入实付总额" />
        </el-form-item>
        <el-form-item label="余额支付金额" prop="balanceAmount">
          <el-input v-model="form.balanceAmount" placeholder="请输入余额支付金额" />
        </el-form-item>
        <el-form-item label="微信支付金额" prop="wxAmount">
          <el-input v-model="form.wxAmount" placeholder="请输入微信支付金额" />
        </el-form-item>
        <el-form-item label="订单类型 1-陪玩 2-商品" prop="type">
          <el-input v-model="form.type" placeholder="请输入订单类型 1-陪玩 2-商品" />
        </el-form-item>
        <el-form-item label="订单状态 0-待支付 1-已支付 2-已退款 3-部分退款 4-已完成 5-已取消" prop="status">
          <el-input v-model="form.status" placeholder="请输入订单状态 0-待支付 1-已支付 2-已退款 3-部分退款 4-已完成 5-已取消" />
        </el-form-item>
        <el-form-item label="支付状态 0-待支付 1-已支付" prop="payStatus">
          <el-input v-model="form.payStatus" placeholder="请输入支付状态 0-待支付 1-已支付" />
        </el-form-item>
        <el-form-item label="退款状态 0-待退款 1-待审核 2-审核不通过 3-审核通过-部分退款 4-审核通过-全部退款" prop="refundStatus">
          <el-input v-model="form.refundStatus" placeholder="请输入退款状态 0-待退款 1-待审核 2-审核不通过 3-审核通过-部分退款 4-审核通过-全部退款" />
        </el-form-item>
        <el-form-item label="支付时间" prop="payTime">
          <el-date-picker clearable v-model="form.payTime" type="date" value-format="yyyy-MM-dd" placeholder="请选择支付时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="退款时间" prop="refundTime">
          <el-date-picker clearable v-model="form.refundTime" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择退款时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="数量" prop="num">
          <el-input v-model="form.num" placeholder="请输入数量" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" v-if="!disabled">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listOrder, getOrder, delOrder, addOrder, updateOrder } from "@/api/biz/order";
import { template } from "@antv/g2plot/lib/utils";

export default {
  name: "Order",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单表格数据
      orderList: [],
      // 弹出层标题
      title: "",
      // 是否禁止表单填写
      disabled: false,
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        type: null,
        status: null,
        payStatus: null,
        refundStatus: null,
      },
      // 表单参数
      form: {},
      // 列信息
      columns: {
        userId: { label: "用户id", visible: true },
        orderNo: { label: "订单号", visible: true },
        thirdOrderNo: { label: "第三方订单号(微信)", visible: true },
        totalAmount: { label: "订单总额", visible: true },
        couponAmount: { label: "优惠金额", visible: true },
        payAmount: { label: "实付总额", visible: true },
        balanceAmount: { label: "余额支付金额", visible: true },
        wxAmount: { label: "微信支付金额", visible: true },
        type: { label: "订单类型 1-陪玩 2-商品", visible: true },
        status: { label: "订单状态 0-待支付 1-已支付 2-已退款 3-部分退款 4-已完成 5-已取消", visible: true },
        payStatus: { label: "支付状态 0-待支付 1-已支付", visible: true },
        refundStatus: { label: "退款状态 0-待退款 1-待审核 2-审核不通过 3-审核通过-部分退款 4-审核通过-全部退款", visible: true },
        payTime: { label: "支付时间", visible: true },
        refundTime: { label: "退款时间", visible: true },
        num: { label: "数量", visible: true },
      },
      // 表单校验
      rules: {
        orderNo: [
          { required: true, message: "订单号不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询订单列表 */
    getList() {
      this.loading = true;
      listOrder(this.formatParamsBatch(this.queryParams)).then(response => {
        this.orderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        orderNo: null,
        thirdOrderNo: null,
        totalAmount: null,
        couponAmount: null,
        payAmount: null,
        balanceAmount: null,
        wxAmount: null,
        type: null,
        status: 1,
        payStatus: 1,
        refundStatus: 1,
        payTime: null,
        refundTime: null,
        num: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.disabled = false;
      this.title = "添加订单";
      this.open = true;
    },
    /** 修改按钮操作 */
    handleUpdate(row, readOnly) {
      this.reset();
      const id = row.id || this.ids
      getOrder(id).then(response => {
        this.form = response.data;
        this.disabled = readOnly ? true : false;
        this.title = readOnly ? "订单详情" : "修改订单";
        this.open = true;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateOrder(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOrder(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除订单编号为"' + ids + '"的数据项？').then(function () {
        return delOrder(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('biz/order/export', {
        ...this.queryParams
      }, `order_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style scoped lang="scss">
.el-date-editor,
.el-select {
  width: 100%;
}
</style>
